package com.xingshang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xingshang.model.po.Goods;
import com.xingshang.model.po.User;
import com.xingshang.service.GoodsService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class IndexController {

    private final GoodsService goodsService;

    @Value("${page.size}")
    private int PAGE_SIZE;


    @RequestMapping("/")
    public String index(Model model, HttpSession session, @RequestParam(defaultValue = "1") int page) {
        //判断是否以登陆
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        IPage<Goods> byPage = goodsService.findByPage(page, PAGE_SIZE);
        List<Goods> HotGoods = goodsService.selectHotAll();
        //jstl
        model.addAttribute("goodsHotList", HotGoods);
        model.addAttribute("goodsNewList", byPage.getRecords());
        model.addAttribute("currentPage", byPage.getCurrent());
        model.addAttribute("totalPages", byPage.getPages());
        model.addAttribute("total", byPage.getTotal());
        return "index";
    }
}
